package com.mjh.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.mjh.bean.User;
import com.mjh.bean.UserRole;
import com.mjh.mapper.UserMapper;
import com.mjh.mapper.UserRoleMapper;
import com.mjh.model.BaseQuery;
import com.mjh.model.SecurityUser;
import com.mjh.service.UserService;
import com.mjh.utils.AuthUtil;
import com.mjh.vo.PieData;
import com.mjh.vo.UserDetailInfoVo;
import com.mjh.vo.UserVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import java.util.Date;
import java.util.List;

@Service
public class UserServiceImpl implements UserService {
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private PasswordEncoder passwordEncoder;
    @Autowired
    private UserRoleMapper userRoleMapper;
    @Override
    public User queryById(Integer id) {
        return userMapper.selectByPrimaryKey(Long.valueOf(id));
    }

    @Override
    public UserDetailInfoVo queryUserDetaillnfoVoById(Integer loginUserId) {
        return userMapper.selectByIdUserVo(loginUserId);
    }

    @Override
    public Boolean updataPwd(User user) {
        // 获取当前登录用户id
        user.setId(AuthUtil.getLoginUserId());
        // 修改时间
        user.setEditTime(new Date());
        // 修改人
        user.setEditBy(AuthUtil.getLoginUserId());
        // 密码加密
        user.setLoginPwd(passwordEncoder.encode(user.getLoginPwd()));
        return userMapper.updateByPrimaryKeySelective(user)>0;
    }

    @Override
    public PageInfo<User> queryUserListPage(Integer current, Integer size) {
        PageHelper.startPage(current,size);
        List<User> pageInfo =  userMapper.selectList(new BaseQuery());

        return new PageInfo<>(pageInfo);
    }

    @Override
    public Boolean modifyUser(User user) {
        user.setEditTime(new Date());
        user.setEditBy(AuthUtil.getLoginUserId());

        return userMapper.updateByPrimaryKeySelective(user)>0;
    }

    @Override
    public Boolean saveUser(User user) {
        user.setCreateTime(new Date());
        user.setCreateBy(AuthUtil.getLoginUserId());
        user.setLoginPwd(passwordEncoder.encode(user.getLoginPwd()));
        return userMapper.insertSelective(user)>0;
    }

    @Override
    public void modifyTime(SecurityUser user) {
        user.setLastLoginTime(new Date());
        User user1 = new User();
        user1.setId(user.getId());
        user1.setLastLoginTime(new Date());
        userMapper.updateByPrimaryKeySelective(user1);
    }

    @Override
    public Boolean removeUserById(Integer id) {

        return userMapper.deleteByPrimaryKey(Long.valueOf(id))>0;
    }

    @Override
    public Boolean removeUserBatchByIds(List<Integer> ids) {

        return userMapper.deleteUserBatchByIds(ids)>0;
    }

    @Override
    public List<User> queryAllUsers() {
        return userMapper.selectListAll();
    }

    @Override
    public List<UserVo> queryAllUserVos() {
        return userMapper.selectAllRole();
    }

    @Override
    public UserVo queryUserVoRoleById(Integer id) {

        return userMapper.selectUserVoRoleById(id);
    }

    @Override
    public List<PieData> queryUserAct() {
        return userMapper.queryUserAct();
    }


}
